package com.example.benefitcommunity.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author makejava
 */
public class MD5Util {
    /**
     * 加密
     */
    public static String encryptMD5(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] digest = md.digest(input.getBytes());
            StringBuilder sb = new StringBuilder();
            for (byte b : digest) {
                sb.append(String.format("%02x", b));
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("Could not find MD5 algorithm", e);
        }
    }

    /**
     * 检验
     */
    public static boolean checkMD5(String input, String encrypted) {
        String encryptedInput = encryptMD5(input);
        return encryptedInput.equals(encrypted);
    }
}
